<?php

use \GatewayWorker\Lib\Gateway;

class GroupMgr
{
	private $_userid_2_groupid = array();

	//主要是用来离线通知，没什么大用处（因为离线时获取不到uid）
	private $_clientid_2_groupid = array();

	private $_group_index = 0;
	public function getActiveGroupId($user_id){
		//没有加过任何组
		$group_id = $this->_userid_2_groupid[$user_id];
		if(!isset($group_id)){
			return 0;
		}

		//这个组的人，全部下线了
		$client_id_count = Gateway::getClientIdCountByGroup($group_id);
		if($client_id_count < 1){
			$this->deleteGroup($user_id);
			return 0;
		}

		//todo : 询问玩家是否重新进房间
		return $group_id;
	}

	public function deleteGroup($user_id){
		unset($this->_userid_2_groupid[$user_id]);
	}

    public function joinGroup($client_id, $group)
    {
    	$user_id = Gateway::getUidByClientId($client_id);
        $this->_userid_2_groupid[$user_id] = $group;

        //设置
        $this->_clientid_2_groupid[$client_id] = array('group_id' => $group, 'user_id' => $user_id); ;
        Gateway::joinGroup($client_id, $group);
    }

    public function leaveGroup($client_id)
    {
        $user_id = Gateway::getUidByClientId($client_id);
        $group_id = $this->_userid_2_groupid[$user_id];
        
        $this->deleteGroup($user_id);
        //清除
        unset($this->_clientid_2_groupid[$client_id]);

       if(!isset($group_id)){
       		echo "fatal error:can not find group_id,client_id=$client_id\n";
       		return false;
       }

        //广播
       $num = Gateway::getClientCountByGroup($group_id);
       $num--;//todo:此种写法有纰漏，后面修改
	   Events::sendToGroup($group_id, "notify_exitroom", array('uid'=>$user_id, 'num' => $num, 'reason'=>'escape'));
        if($num < 1){
            //清除正在战斗的房间
            unset($_groupid_2_personwrap[$group_id]);
        }
        Gateway::leaveGroup($client_id, $group_id);

        return true;
    }


    public function leaveGroupWhenOffline($client_id)
    {
        $wrap = $this->_clientid_2_groupid[$client_id];
        if(!isset($wrap)){
       		return;
        }

        //清除
        unset($this->_clientid_2_groupid[$client_id]);

        $group_id = $wrap['group_id'];
        $user_id = $wrap['user_id'];

        $num = Gateway::getClientCountByGroup($group_id);
        if($num < 1){
            echo "leaveGroupWhenOffline num=$num, group_id=$group_id\n";
            //清除正在战斗的房间
            unset($_groupid_2_personwrap[$group_id]);
            return;
        }
        //广播
		Events::sendToGroup($group_id, "notify_exitroom", array('uid'=>$user_id, 'num' => $num, 'reason'=>'offline'));
    }


    public function getGroupId($client_id){
    	$user_id = Gateway::getUidByClientId($client_id);
    	return $this->_userid_2_groupid[$user_id];
    }

    public function createGroupId(){
    	$this->_group_index++;
        if($this->_group_index > 147483647)
            $this->_group_index = 1;
    	return $this->_group_index;
    }

    //=========记录正在战斗的房间 Start=========
    //  $msg_person['persons'][] = array('uid'=>$uid, 'name'=>$name, 'state'=>2, 'content'=>"1,1,1,1,1");//state = 2是 客户端的open状态
    private $_groupid_2_personwrap = array();
    public function startBattle($group_id, $person_wrap){//$person_wrap = $msg_body['persons'][] = array('uid'=>$uid, 'name'=>$name);
        $this->_groupid_2_personwrap[$group_id] = $person_wrap;
    }
    public function isBattle($group_id){
        $person_wrap = $this->_groupid_2_personwrap[$group_id];
        return isset($person_wrap);
    }
    //return list
    public function &getBattlePersonWrap($group_id){
        return $this->_groupid_2_personwrap[$group_id];
    }
    //=========记录正在战斗的房间 End=========
}